package com.yuanian.timingwheel.config;

import io.ebean.Database;
import io.ebean.DatabaseFactory;
import io.ebean.config.DatabaseConfig;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class EbeanConfig {

    final DataSource dataSource;

    public EbeanConfig(@Qualifier("dataSource") DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Bean
    public Database ebeanServer() {
        DatabaseConfig config = new DatabaseConfig();
        config.setDataSource(dataSource);
        config.setDefaultServer(true);
        config.setRegister(true);
        //是否生成建表SQL
        config.setDdlGenerate(true);
        //是否跳过建表SQL
        config.setDdlCreateOnly(false);
        //是否执行建表SQLs，不建议设置为true
        config.setDdlRun(false);
        return DatabaseFactory.create(config);
    }
}
